package top.linuyx.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author Linuyx
 * @Description WebMvc配置类
 * @Date Created in 2021-04-08 15:46
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.image.uploadPath}")
    private String imageUploadPath;

    @Value("${file.image.accessPath}")
    private String imageAccessPath;

    /**
     * 服务器资源路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //图片资源
        registry.addResourceHandler(imageAccessPath + "/**")
                .addResourceLocations("file:" + imageUploadPath);
    }

    /**
     * 跨域
     *
     * @param registry CorsRegistry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
                .maxAge(3600);
    }
}
